package com.jwx.mycar.controller;

import com.jwx.mycar.common.ServiceException;
import com.jwx.mycar.entity.po.Picture;
import com.jwx.mycar.entity.po.Recognition;
import com.jwx.mycar.entity.po.Stat;
import com.jwx.mycar.entity.po.User;
import com.jwx.mycar.entity.vo.ResultVo;
import com.jwx.mycar.service.PictureService;
import com.jwx.mycar.service.RecognitionService;
import com.jwx.mycar.service.StatService;
import com.jwx.mycar.utils.AIImage;
import com.jwx.mycar.utils.FileUtils;
import com.jwx.mycar.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(tags = "图片识别")
@RequestMapping("/ai")
@RestController
public class IdentifyController {
	@Autowired
	private PictureService pictureService;

	@Autowired
	private RecognitionService recognitionService;

	@Autowired
	ResultVOUtil resultVOUtil;

	@Autowired
	StatService statService;
	// 文件上传接口
    @ApiOperation("")
	@PostMapping("/identify")
	public ResultVo upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
		System.out.println(request.getSession().getId());
		if (file.isEmpty()) {
			return resultVOUtil.success("文件为空");
		}
		User user = (User)request.getSession().getAttribute("user");
//		System.out.println(user);
		Map<String, Object> map = new HashMap<String, Object>();
		Picture picture= new Picture();
		Stat stat=new Stat();
		try {
			picture = FileUtils.upload(file);
			System.out.println(picture);
			// 将文件信息保存到数据库
			picture.setUserId(user.getUserId());
		//	picture.setUserId(userId);
			// 百度识别
			List<Recognition> result = AIImage.classify(picture.getPicPath());
			if (!StringUtils.isEmpty(picture.getPicPath())) {
				String path = picture.getPicPath().replace('\\', '/');
				picture.setPicPath(path);
			}
			picture.setPicPath(picture.getPicPath().substring(7));
			map.put("picture", picture);
			map.put("username", user.getUsername());
			map.put("userId",user.getUserId());
			//map.put("username",username);
			map.put("resultList", result);
			pictureService.save(picture);
			for (int i = 0; i < result.size(); i++) {
				result.get(i).setPicId(picture.getPicId());
			}
			recognitionService.save(result);
		} catch(ServiceException | JSONException e) {
			return resultVOUtil.fail(e.getMessage());
		}

		System.out.println(map);
		// 文件上传成功后返回文件信息
		return resultVOUtil.success(map);
	}
	@ApiOperation("根据url检测")
	@PostMapping("/detectByUrl")
	@ApiImplicitParam(name = "url" ,value = "图片网络地址")
	public List<Recognition> test(@RequestParam("url") String url){
		return pictureService.detectImage(url);
	}
//	@RequestMapping("/api/file/list")
//	@ResponseBody
//	public ResultVo getFileList(Integer pageNum, Integer pageSize, HttpServletRequest request) {
//		if (pageNum == null || pageNum <= 1) pageNum = 1;
//		if (pageSize == null || pageSize <= 0 || pageSize >= 10) pageSize = 10;
//		User user = (User)request.getSession().getAttribute("user");
//		int userId = 0;
//		if (user != null) userId = user.getUserId();
//		int count = pictureService.getCount();
//		List<PicturePO> list = pictureService.find(pageNum, pageSize, userId);
//		// 修改所有斜杠问题
//		for (PicturePO pp : list) {
//			if (!StringUtils.isEmpty(pp.getPicPath())) {
//				String path = pp.getPicPath().replace('\\', '/');
//				pp.setPicPath(path);
//			}
//		}
//		HashMap<String, Object> map = new HashMap<String, Object>();
//		map.put("count", count);
//		map.put("list", list);
//
//		return ResultGenerator.genSuccessResult(map);
//	}
//
//	/**
//	 * 点赞图片
//	 */
//	@ApiOperation(value = "点赞", notes = "传picId")
//	@ApiImplicitParam(name = "picId", value = "picId", required = true, dataType = "String", paramType = "query")
//	@PostMapping("up")
//	public ResultVo upFile(String picId, HttpServletRequest request) {
//
//		if (picId == null) return resultVOUtil.fail("图片id不能为空");
//		User user = (User)request.getSession().getAttribute("user");
//		int count = pictureService.upFile(picId, user.getUserId());
//		return resultVOUtil.success(count);
//	}
//
//	/**
//	 * 点踩图片
//	 */
//	@PostMapping("/api/file/down")
//	@ResponseBody
//	public Result downFile(Integer picId, HttpServletRequest request) {
//		if (picId == null) return ResultGenerator.genFailResult("图片id不能为空");
//		User user = (User)request.getSession().getAttribute("user");
//		int count = pictureService.downFile(picId, user.getUserId());
//		return ResultGenerator.genSuccessResult(count);
//	}
}
